package cn.org.donald.controller.system;


import cn.org.donald.framework.constant.HttpStatus;
import cn.org.donald.framework.handler.ServiceException;
import cn.org.donald.framework.pojo.vo.TableResult;
import cn.org.donald.pojo.dto.system.LogSqlGroupByMethodDto;
import cn.org.donald.pojo.dto.system.QueryLogSqlByMethodDto;
import cn.org.donald.pojo.entity.system.LogSql;
import cn.org.donald.pojo.vo.LogSqlGroupByMethodVo;
import cn.org.donald.service.system.LogSqlService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import cn.org.donald.framework.base.BaseController;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Donald
 * @since 2021-01-09
 */
@RestController
@RequestMapping("/log-sql")
@Api(value = "sql日志")
public class LogSqlController extends BaseController {

    @Reference(version = "1.0.0", group = "donald")
    private LogSqlService logSqlService;

    @PostMapping("/page-group")
    @ApiOperation(value = "分页获取sql日志汇总")
    public TableResult pageGroup(@Validated @RequestBody LogSqlGroupByMethodDto logSqlGroupByMethodDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new ServiceException(HttpStatus.INTERNAL_SERVER_ERROR, Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        IPage<LogSqlGroupByMethodVo> page = logSqlService.selectPageGroup(logSqlGroupByMethodDto);
        return toTableResult(page);
    }

    @PostMapping("/getByAndMethod")
    @ApiOperation(value = "根据方法名获取sql日志")
    public TableResult getByMethod(@Validated @RequestBody QueryLogSqlByMethodDto queryLogSqlByMethodDto,BindingResult bindingResult) {
        IPage<LogSql> page = logSqlService.selectByApplicationAndMethod(queryLogSqlByMethodDto);
        return toTableResult(page);
    }
}

